import { computed } from 'vue'
import { usePermissionStore } from '@/stores/permission'

/**
 * 权限相关的组合式函数
 */
export function usePermissions() {
  const permissionStore = usePermissionStore()

  // 用户按钮权限
  const userButtons = computed(() => permissionStore.userButtons)
  
  // 用户菜单权限
  const userMenus = computed(() => permissionStore.userMenus)
  
  // 用户信息
  const userInfo = computed(() => permissionStore.userInfo)

  // 检查单个权限
  const hasPermission = (permission) => {
    if (!permission) return true
    return permissionStore.hasPermission.value(permission)
  }

  // 检查是否拥有任意一个权限
  const hasAnyPermission = (permissions) => {
    if (!permissions || permissions.length === 0) return true
    return permissionStore.hasAnyPermission.value(permissions)
  }

  // 检查是否拥有所有权限
  const hasAllPermissions = (permissions) => {
    if (!permissions || permissions.length === 0) return true
    return permissionStore.hasAllPermissions.value(permissions)
  }

  // 检查菜单权限
  const hasMenuPermission = (menuPath, menus = null) => {
    if (!menuPath) return false
    
    const menuList = menus || userMenus.value
    if (!menuList || menuList.length === 0) return false
    
    // 递归检查菜单树
    function checkMenuTree(menuItems, targetPath) {
      for (const menu of menuItems) {
        // 检查当前菜单
        if (menu.path === targetPath) {
          return true
        }
        
        // 检查子菜单
        if (menu.children && menu.children.length > 0) {
          if (checkMenuTree(menu.children, targetPath)) {
            return true
          }
        }
      }
      return false
    }
    
    return checkMenuTree(menuList, menuPath)
  }

  // 获取用户权限列表
  const getUserPermissions = () => {
    return permissionStore.getUserPermissions()
  }

  // 获取用户菜单列表
  const getUserMenus = () => {
    return permissionStore.getUserMenus()
  }

  // 获取用户信息
  const getUserInfo = () => {
    return permissionStore.getUserInfo()
  }

  // 刷新权限
  const refreshPermissions = async () => {
    return await permissionStore.refreshPermissions()
  }

  return {
    // 响应式数据
    userButtons,
    userMenus,
    userInfo,
    
    // 权限检查方法
    hasPermission,
    hasAnyPermission,
    hasAllPermissions,
    hasMenuPermission,
    
    // 数据获取方法
    getUserPermissions,
    getUserMenus,
    getUserInfo,
    
    // 其他方法
    refreshPermissions
  }
}

// 导出权限检查函数，用于模板中直接使用
export const hasPermission = (permission) => {
  const { hasPermission: checkPermission } = usePermissions()
  return checkPermission(permission)
}

export const hasAnyPermission = (permissions) => {
  const { hasAnyPermission: checkAnyPermission } = usePermissions()
  return checkAnyPermission(permissions)
}

export const hasAllPermissions = (permissions) => {
  const { hasAllPermissions: checkAllPermissions } = usePermissions()
  return checkAllPermissions(permissions)
}

export const hasMenuPermission = (menuPath, menus = null) => {
  const { hasMenuPermission: checkMenuPermission } = usePermissions()
  return checkMenuPermission(menuPath, menus)
}